/*
 * Atomic Predicates for Transformers
 * 
 * Copyright (c) 2015 UNIVERSITY OF TEXAS AUSTIN. All rights reserved. Developed
 * by: HONGKUN YANG and SIMON S. LAM http://www.cs.utexas.edu/users/lam/NRL/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * with the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimers.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimers in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the UNIVERSITY OF TEXAS AUSTIN nor the names of the
 * developers may be used to endorse or promote products derived from this
 * Software without specific prior written permission.
 * 
 * 4. Any report or paper describing results derived from using any part of this
 * Software must cite the following publication of the developers: Hongkun Yang
 * and Simon S. Lam, Scalable Verification of Networks With Packet Transformers
 * Using Atomic Predicates, IEEE/ACM Transactions on Networking, October 2017,
 * Volume 25, No. 5, pages 2900-2915 (first published as IEEE Early Access
 * Article, July 2017, Digital Object Identifier: 10.1109/TNET.2017.2720172).
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
 * THE SOFTWARE.
 */
package transformer;

import java.io.IOException;

import common.FWDAPSet;
import common.PositionTuple;

public class TreeStat {

    // statistics we want to know for a given reachability tree
    private int num_of_leaves;
    private double avg_path_len;
    private int max_path_len;

    // helper variables
    private int num_of_edges;

    /**
     * Constructor. Given a node in the reachability tree as the root. Compute statistics.
     * @param root
     */
    public TreeStat(AbstractState root){
        num_of_leaves = 0;
        max_path_len = 0;
        num_of_edges = 0;

        if(root == null){
            System.err.println("The root node is NULL!");
            return;
        }

        ComputeStat(root, 0);

        avg_path_len = num_of_edges/(num_of_leaves + 0.0);
    }

    // recursively compute tree statistics. When a leaf node is reached, update
    // num_of_leaves, max_path_len, and resets current_path_len. When a new child
    // is reached, inc current_path_len and num_of_edges.
    private void ComputeStat(AbstractState node, int current_path_len) {
        if(node.nextState == null || node.nextState.size() == 0){
            num_of_leaves ++;

            if(current_path_len > max_path_len) {
                max_path_len = current_path_len;
            }
            return;
        }

        for(AbstractState s : node.nextState) {
            num_of_edges ++;
            ComputeStat(s, current_path_len + 1);
        }
    }

    public int GetNumOfLeaves() {
        return num_of_leaves;
    }

    public double GetAvgPathLen() {
        return avg_path_len;
    }

    public int GetMaxPathLen() {
        return max_path_len;
    }

    public void ShowStat(){
        System.out.println("number of leaves: " + num_of_leaves);
        System.out.println("avg path length " + avg_path_len);
        System.out.println("max path length " + max_path_len);
    }

    public void Tests(){
        // do some testing
        StateAP root = new StateAP(new PositionTuple("1", "2"), new FWDAPSet(1));

        TreeStat ts = new TreeStat(root);
        System.out.println(ts.GetAvgPathLen());
        System.out.println(ts.GetMaxPathLen());
        System.out.println(ts.GetNumOfLeaves());

        StateAP n1 = new StateAP(new PositionTuple("2", "3"), new FWDAPSet(1));
        StateAP n2 = new StateAP(new PositionTuple("2", "4"), new FWDAPSet(1));
        StateAP n3 = new StateAP(new PositionTuple("3", "5"), new FWDAPSet(1));

        n1.addNextState(n3);

        root.addNextState(n1);
        root.addNextState(n2);

        /**
         * root->n1->n3
         *   \->n2
         */
        ts = new TreeStat(root);
        System.out.println(ts.GetAvgPathLen());
        System.out.println(ts.GetMaxPathLen());
        System.out.println(ts.GetNumOfLeaves());
    }

    public static void ShowProviderConeTree(){
        // 52941
        String net_name = "37684";
        String tunnel_file = "37684_mpls_tunnels";
        //String tunnel_file = "52941_52941_mpls_tunnels";

        int number = 40;

        NetworkCOVEMPLSAP net;
        try {
            net = new NetworkCOVEMPLSAP(net_name, null, net_name);


            net.read_tunnels_from_file(tunnel_file);

            net.ap_mpls(number);

            net.compute_trees(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){

        ShowProviderConeTree();
    }

}
